/**
 * Created by meiguizhinian on 2018/7/17
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.BaseObject = ivy.Node.extend({
	_className:"ivy.BaseObject",
	ctor: function () {
		this._super();

		this.objDir_ = ivy.BaseObject.Dir.eRight;

		this.objPosition_ = cc.p();
		this.objBornPosition_ = cc.p();

		this.activeBox_  = cc.rect();
		this.activeBoxSrc_  = cc.rect();
		this.colBox_  = cc.rect();
		this.atkBox_  = cc.rect();

		this.objPropertyMap_ = new LLDictionary();
	},
	getObjectType : function() {
		return ivy.BaseObject.ObjTypeBase;
	},

	initWithActorID : function(actorID) {
		this.objActorID_ = actorID;
		if (actorID >= 0) {
			let sdm = ivy.SceneDataManager.getInstance();
			this.objClassID_ = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eClassID);
			this.objScriptID_ = sdm.getObjectBaseInfo(this.objActorID_,ivy.BaseObject.ObjectBaseInfo.eScriptID);
			this.objActionID_ = sdm.getObjectBaseInfo(this.objActorID_,ivy.BaseObject.ObjectBaseInfo.eActionID);
			this.objActionID_Bak_ = this.objActionID_;
			this.objLayerID_ = sdm.getObjectBaseInfo(this.objActorID_,ivy.BaseObject.ObjectBaseInfo.eLayerID);
			this.objBasicFlag_=sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eFlag);
			this.objAnimationDataName_ = sdm.getObjectClassName(this.objClassID_);
			let sx = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eBornPosX);
			let sy = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eBornPosY);
			this.objBornPosition_ = cc.p(sx,sy);
			this.objPosition_ = cc.p(sx,sy);
			let ax0 = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eActiveX0);
			let ay0 = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eActiveY0);
			let ax1 = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eActiveX1);
			let ay1 = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eActiveY1);
			this.objDataID_ = sdm.getObjectBaseInfo(this.objActorID_, ivy.BaseObject.ObjectBaseInfo.eDataID);
			this.activeBoxSrc_= cc.rect(ax0-sx,ay0-sy,ax1-ax0,ay1-ay0);
			this.objDataType_ = sdm.getObjectDataTypeByActorID(this.objActorID_);
			this.activeBox_.x += this.objPosition_.x;
			this.activeBox_.y += this.objPosition_.y;
		} else {
			return false;
		}
		return true;
	},

	initAfterCreate : function() {},

	getObjectActorID : function() {
		return this.objActorID_;
	},

	getObjectAIID : function() {
		return this.objAIID_;
	},

	getObjectClassID : function() {
		return this.objClassID_;
	},
	getObjectScriptID : function() {
		return this.objScriptID_;
	},

	getScanSubScriptIndex : function() {
		return this.objScanSubScriptIndex_;
	},
	setScanSubScriptIndex : function(index) {
		this.objScanSubScriptIndex_ = index;
	},

	getScriptConditionIndex : function() {
		return this.objScriptConditionIndex_;
	},
	setScriptConditionIndex : function(index) {
		this.objScriptConditionIndex_ = index;
	},

	getScriptRunning : function() {
		return this.objScriptRunning_;
	},
	setScriptRunning : function(isRunning) {
		this.objScriptRunning_ = isRunning;
	},

	setObjectActionID : function(id) {
		if (this.objAniPlayer_) {
			this.objActionID_Bak_ = this.objAniPlayer_.getActionID();
			this.objAniPlayer_.setActionID(id);
			this.objActionID_ = id;
		}
	},
	getObjectActionID : function() {
		return this.objActionID_;
	},

	getObjectActionIDBak : function() {
		return this.objActionID_Bak_;
	},


	getObjectLayerID : function() {
		return this.objLayerID_;
	},

	getObjectLinkActorID : function() {
		return this.objLinkActorID_;
	},

	getObjectGroupID : function() {
		return this.objGroupID_;
	},
	setObjectGroupID : function(id) {
		this.objGroupID_ = id;
	},

	getObjectDir : function() {
		return this.objDir_;
	},

	setObjectDir : function(dir) {
		this.objDir_ = dir;
		if (this.objAniPlayer_) {
			if (this.objDir_ === ivy.BaseObject.Dir.eRight) {
				this.objAniPlayer_.setFlippedX(false);
			} else if (this.objDir_ === ivy.BaseObject.Dir.eLeft) {
				this.objAniPlayer_.setFlippedX(true);
			}
		}
	},

	getObjectPosition : function() {
		return this.objPosition_;
	},

	getObjectScreenPosition : function() {
		let screenPos = this.getPosition();
		let cameraOffset = ivy.GameCameraManager.getInstance().getCameraOffset();
		screenPos = cc.pSub(screenPos, cameraOffset);
		return screenPos;
	},

	getObjectCameraPosition : function() {
		return this.objPosition_;
	},

	setObjectPosition : function(pos) {
		this.objPosition_ = pos;
	},

	getObjectBornPostion : function() {
		return this.objBornPosition_;
	},

	getObjectAniPlayer : function() {
		return this.objAniPlayer_;
	},

	setObjectDataType : function(t) {
		this.objDataType_ = t;
	},

	getObjectDataType : function() {
		return this.objDataType_;
	},
	getObjectDataID : function() {
		return this.objDataID_;
	},

	checkBasicFlag : function(flag) {
		return (this.objBasicFlag_ & flag) === flag;
	},

	getObjectPropertyMap : function() {
		return this.objPropertyMap_;
	},

	getObjectProperty : function(propertyID) {
		return this.objPropertyMap_.get(propertyID) || -1.0;
	},

	setObjectProperty : function(propertyID, val) {
		this.objPropertyMap_.set(propertyID, val);
	},

	isObjectPropertyExist : function(propertyID) {
		return this.objPropertyMap_.has(propertyID);
	},

	changeObjectProperty : function(propertyID, val) {
		if (this.objPropertyMap_.has(propertyID)) {
			this.objPropertyMap_.set(propertyID, this.objPropertyMap_.get(propertyID)+val);
		}
	},

	getObjectForceZOrder : function() {
		return this.objForceZOrder_;
	},

	setObjectForceZOrder : function(z) {
		this.objForceZOrder_ = z;
	},

	getVelocityInXAxis : function() {
		return this.objVelocityInXAxis_;
	},
	getVelocityInYAxis : function() {
		return this.objVelocityInYAxis_;
	},
	addVelocityInXAxis : function(s) {
		this.objVelocityInXAxis_ += s;
	},
	addVelocityInYAxis : function(s) {
		this.objVelocityInYAxis_ += s;
	},

	resetVelocity : function() {
		this.objVelocityInXAxis_ = 0;
		this.objVelocityInYAxis_ = 0;
	},

	getActivateBox : function() {
		this.activeBox_ = cc.rect(this.activeBoxSrc_);
		this.activeBox_.x += this.objPosition_.x;
		this.activeBox_.y += this.objPosition_.y;
		return this.activeBox_;
	},

	getAttackBox : function() {return cc.rect();},
	getCollisionBox : function() {return cc.rect();},

	initAI : function() {},
	doAILogic : function() {},
	doScriptLogic : function() {},

	isKeyFrameDataNeedToDeal : function() {return false;},
	doKeyFrameLogicWithOutCheckSamePlaneTo : function(obj) {},
	doKeyFrameLogicWithOnlyCheckSamePlaneTo : function(obj) {},
	doKeyFrameLogicTo : function(obj) {return false;},
	checkSamePlane : function(obj) {return false;},
	getObjectTypeString : function() {return "baseObject";},

	isDead : function() {
		return this.isDead_;
	},
	setDead : function(dead) {
		this.isDead_ = dead;
		this.setVisible(!this.isDead_);
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	//脚本相关
	objScriptRunning_ : false,
	objScanSubScriptIndex_ : 0,
	objScriptConditionIndex_ : 0,
	//////////////////////////////////////////////////////////////////////////
	objActorID_     : -1,
	objLinkActorID_ : -1,
	objAIID_        : -1,
	objClassID_     : -1,
	objScriptID_    : -1,
	objAnimationID_ : -1,
	objActionID_    : -1,
	objActionID_Bak_:  -1,
	objLayerID_     : -1,
	objBasicFlag_   :  0,
	objGroupID_     : -1,
	objDataType_    :  -1,
	objDataID_      :  -1,
	objForceZOrder_ :  -1,
	objDir_ : 0,//ivy.BaseObject.Dir.eRight
	objAnimationDataName_ : "",
	objPosition_ : null,//cocos2d::Point
	objBornPosition_ : null,//cocos2d::Point
	objAniPlayer_: null,//AniPlayer*
	objVelocityInXAxis_ : 0,
	objVelocityInYAxis_ : 0,
	objVelocityInXAir_ : 0,
	objVelocityInYAir_ : 0,
	activeBox_ : null,//cocos2d::Rect
	activeBoxSrc_ : null,//cocos2d::Rect
	colBox_ : null,//cocos2d::Rect
	atkBox_ : null,//cocos2d::Rect
	objPropertyMap_ : null,//std::unordered_map<int32_t, float>
	//////////////////////////////////////////////////////////////////////////
	isDead_ : false,
});

ivy.BaseObject.create = function () {
	return new ivy.BaseObject();
};

ivy.BaseObject.ObjTypeBase = 0;


ivy.BaseObject.Dir = {
	eLeft       : 0,
	eRight      : 1,
	eEnumCount  : 2,
};

ivy.BaseObject.ObjectBaseInfo = {
	eActorID    : 0,
	eClassID    : 1,
	eFlag       : 2,
	eParaSize   : 3,
	eLayerID    : 4,
	eScriptID   : 5,
	eActionID   : 6,
	eBornPosX   : 7,
	eBornPosY   : 8,
	eActiveX0   : 9,
	eActiveY0   : 10,
	eActiveX1   : 11,
	eActiveY1   : 12,
	eDataID     : 13,
	eEnumCount  : 14,
};